package utils

import (
	"github.com/akkuman/parseConfig"
	"github.com/globalsign/mgo"
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

type Mongo struct {
	Url string
}

type Mysql struct {
	Host string
}

func GetFilePath(str string) string {
	config := parseConfig.New("src/code/mongo/config/filepath.json")
	path := config.Get(str)
	return path.(string)
}

func getConfig(path string) (*parseConfig.Config) {
	config := parseConfig.New(path)
	return &config
}

func MysqlCon() (*sql.DB) {
	m := Mysql{}
	config := getConfig(GetFilePath("config"))
	m.Host = config.Get("mysql > host").(string)
	db, err := sql.Open("mysql", m.Host)
	if err != nil {
		Error.Println("mysql conn error " ,err) // Just for example purpose. You should use proper error handling instead of panic
	}
	return db
}

func MongoCon() (*mgo.Session) {
	r := Mongo{}
	r.Url = getConfig(GetFilePath("config")).Get("mongo > host").(string)
	session, err := mgo.Dial(r.Url)
	if nil != err {
		Error.Println("Mongo dial error ",err)
	}

	return session

}
